<?php
namespace app\daohang\event;

use think\Controller;

class Info extends Controller
{
    public function _initialize()
    {
        $this->query = $this->request->param();
        
        parent::_initialize();
    }
    
    public function save()
    {
        if( !daohangSave(input('post.'), true) ){
            $this->error(\daicuo\Info::getError());
        }

        $this->success(lang('success'));
    }
    
    public function delete()
    {
        daohangDelete(input('id/a'));
        
        $this->success(lang('success'));
    }
    
    public function update()
    {
        if( !daohangUpdate(input('post.'), true) ){
            $this->error(\daicuo\Info::getError());
        }
        $this->success(lang('success'));
    }
    
    public function status()
    {
        if( !$ids = input('post.id/a') ){
            $this->error(lang('errorIds'));
        }
        $data = [];
        $data['info_status'] = input('request.value/s', 'hidden');
        foreach($ids as $key=>$id){
            \daicuo\Info::update(['info_id'=>$id], $data, '');
        }
        $this->success(lang('success'));
    }
    
    public function type()
    {
        if( !$ids = input('post.id/a') ){
            $this->error(lang('errorIds'));
        }
        $data = [];
        $data['info_type'] = input('request.value/s', 'index');
        foreach($ids as $key=>$id){
            \daicuo\Info::update(['info_id'=>$id], $data, '');
        }
        $this->success(lang('success'));
    }
    
    public function preview()
    {
        $id = input('id/d',0);
        if(!$id){
            $this->error(lang('errorIds'));
        }
        //查询数据
        $data = daohangGet(['cache'=>false,'id'=>$id]);
        if( $data['info_status'] != 'normal' ){
            $this->error(lang('dh_error_info_hidden'));
        }
        //去掉后台入口文件
        $url = str_replace($this->request->baseFile(), '', daohangUrlInfo($data));
        //跳转到前台预览
        $this->redirect($url,302);
    }
}